#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "command_parse.h"

int command_parse(const char *command, int *argc, char *argv[]) {
  *argc = 0;
  char param[512];
  const char *p = command;

  while (p && *p) {
    while (p && *p && *p == ' ') {
      ++p;
    }

    int ret = sscanf(p, "%s", param);
    if (ret == EOF) {
      break;
    }
    /* if (argv[*argc]) { */
    /*   free(argv[*argc]); */
    /* } */
    argv[*argc] = malloc(strlen(param) + 1);
    strcpy(argv[*argc], param);
    ++*argc;

    p += strlen(param);
  }

  return 0;
}
